local hx__xianji = fk.CreateSkill{
  name = "hx__xianji",
  tags = { Skill.Compulsory },
}


local spec1 = {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__xianji.name) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local all_cards = room:getNCards(player.maxHp)
    local types = {}
    if data.card.type == Card.TypeTrick then
      types = {"basic","equip"}
    elseif data.card.type == Card.TypeEquip then
      types = {"basic","trick"}
    elseif data.card.type == Card.TypeBasic then
      types = {"trick","equip"}
    end
    local cardmap = room:askToArrangeCards(player, {
      skill_name = hx__xianji.name,
      card_map = {all_cards, {}, "Top", "toObtain"},
      free_arrange = false,
      max_limit = {#all_cards, 1},
      min_limit = {0, 0},
      pattern = ".|.|.|.|.|"..table.concat(types, ","),
    })
    for i = #cardmap[1], 1, -1 do
      table.removeOne(room.draw_pile, cardmap[1][i])
      table.insert(room.draw_pile, 1, cardmap[1][i])
    end
    if #cardmap[2] > 0 then
      room:obtainCard(player, cardmap[2][1], false, fk.ReasonJustMove, player, hx__xianji.name)
      local id = Fk:getCardById(cardmap[2][1])
      room:addCardMark(id, "@@hx__xianji1-inhand")
    end
  end,
}

local spec2 = {
  anim_type = "drawcard",
  priority = 0.5,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.suit ~= Card.NoSuit 
    and not table.contains(player:getTableMark("@hx__xianji-turn"), data.card:getSuitString(true))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local suit = data.card.suit
    room:addTableMark(player, "@hx__xianji-turn", data.card:getSuitString(true))
    local card1 = table.filter(room.draw_pile, function(id)
      return Fk:getCardById(id).suit == suit
    end)
    if #card1 > 0 then
      local card = Fk:getCardById(table.random(card1))
      room:obtainCard(player, card, false, fk.ReasonPrey, player, hx__xianji.name)
      room:addCardMark(card, "@@hx__xianji2-inhand")
    else
      local card1_2 = table.filter(room.discard_pile, function(id)
        return Fk:getCardById(id).name == suit
      end)
      if #card1_2 > 0 then
        local card = Fk:getCardById(table.random(card1_2))
        room:obtainCard(player, card, false, fk.ReasonPrey, player, hx__xianji.name)
        room:addCardMark(card, "@@hx__xianji2-inhand")
      end
    end
  end,
}

hx__xianji:addEffect(fk.CardUsing, spec1)
hx__xianji:addEffect(fk.CardResponding, spec1)
hx__xianji:addEffect(fk.CardUsing, spec2)
hx__xianji:addEffect(fk.CardResponding, spec2)


hx__xianji:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return card:getMark("@@hx__xianji2-inhand") > 0
  end,
})

hx__xianji:addEffect("targetmod", {
    bypass_times = function(self, player, skill, scope, card, to)
      return card and
        table.every(Card:getIdList(card), function (id)
          return Fk:getCardById(id):getMark("@@hx__xianji1-inhand") > 0
        end)
    end,
    bypass_distances = function(self, player, skill, card, to)
        return card and
        table.every(Card:getIdList(card), function (id)
            return Fk:getCardById(id):getMark("@@hx__xianji1-inhand") > 0
          end)
      end,
  })

return hx__xianji